package com.example.zhish.scrollview_demo001;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView textView;
    private ScrollView scrollView;
    private Button up;
    private Button down;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv1);
        textView.setText(R.string.tv_text);
        up = (Button) findViewById(R.id.upButton);
        down = (Button) findViewById(R.id.down);

        up.setOnClickListener(this);
        down.setOnClickListener(this);

        scrollView = (ScrollView) findViewById(R.id.scroll);

        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:

                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        /**
                         * （1）getScrollY()---滚动条滑动的距离
                         * （2）getMeasuredHeight()---包含隐藏部分
                         * （3）getHeight()---不包含隐藏部分
                         */

                        //顶部状态
                        if(scrollView.getScrollY()<=0){
                            Log.i("mike","滑动到顶部。。。。。。");
                            Toast.makeText(MainActivity.this,"顶部。。。。。。",Toast.LENGTH_SHORT).show();
                        }

                        //底部状态
                        //TextView的高度<=屏幕的高度+滚动条的滑动距离
                        if(scrollView.getChildAt(0).getMeasuredHeight()<=(scrollView.getHeight()+scrollView.getScrollY())){

                            Log.i("mike","滑动到底部。。。。");

                            Toast.makeText(MainActivity.this,"底部。。。。。。。",Toast.LENGTH_SHORT).show();

                            textView.append(getResources().getString(R.string.tv_text));
                        }

                        break;

                }

                return false;
            }
        });

        /*
        动态隐藏滚动条效果
        scrollView.setHorizontalScrollBarEnabled(false);
        scrollView.setVerticalScrollBarEnabled(false);
        */



    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            //scrollTo()参考点是起始点
            //scrollBy()参考点是上一次的位置
            case R.id.upButton:

                scrollView.scrollBy(0,-30);

                break;
            case R.id.down:
                scrollView.scrollBy(0,30);
                break;
        }
    }
}
